<template>
  <div>
    <!-- 渲染角色列表 -->
    <!-- checkbox 标签内容如果无，就用label属性值作为按钮后面的介绍-->
    <!-- 当checkbox选中时，会把label的值添加到v-model数组里，如果未选中就会从数组里移除 -->
    <!-- 数据回显
         问题：怎么让checkbox默认勾选
         答案：v-model对应数组里有一些值，(这些值会跟循环checkbox的label值配对)，配对成功，影响checkbox勾选状态
     -->
    <el-checkbox-group v-model="roleIdList">
      <el-checkbox
        v-for="item in allRoleList"
        :key="item.id"
        :label="item.id"
      ><span>{{ item.name }}</span></el-checkbox>

      <el-checkbox
        label="禁用"
        disabled
      />
      <el-checkbox
        label="选中且禁用"
        disabled
      />
    </el-checkbox-group>

    <el-row
      class="footer"
      type="flex"
      justify="center"
    >
      <el-col :span="6">
        <el-button
          type="primary"
          size="small"
          @click="confirmDialogFn"
        >确定</el-button>
        <el-button
          size="small"
          @click="cancleDialogFn"
        >取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: 'AssignRole',
  props: {
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    },
    allRoleList: {
      type: Array,
      default: () => {}
    }
  },
  data() {
    return {
      roleIdList: [] // 收集选中角色id的信息
    }
  },
  methods: {
    // 取消弹框
    cancleDialogFn() {
      // this.$emit('showEV', false)
      this.$emit('update:show', false)
    },
    // 确认弹框
    confirmDialogFn() {
      // this.$emit('showEV', false)
      this.$emit('update:show', false)
      this.$emit('addRoleEv', this.roleIdList)
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  margin-top: 30px;
}
</style>
